/**
 * FunctionExpression.java
 * Copyright (C) 2011 Yuanyan<yuanyan.cao@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package edu.rabbit.kernel.schema;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.antlr.runtime.tree.CommonTree;

import edu.rabbit.DbException;
import edu.rabbit.schema.IExpression;
import edu.rabbit.schema.IFunctionExpression;

/**
 * @author Yuanyan<yanyan.cao@gmail.com>
 * 
 */
public class FunctionExpression extends Expression implements IFunctionExpression {

    private final String name;
    private final boolean distinct, all;
    private final List<IExpression> arguments;

    public FunctionExpression(CommonTree ast) throws DbException {
        assert "function_expression".equalsIgnoreCase(ast.getText());
        name = ast.getChild(0).getText();
        if ("*".equals(ast.getChild(1).getText())) {
            all = true;
            distinct = false;
            arguments = Collections.emptyList();
        } else {
            all = false;
            int idx = 1;
            CommonTree child = (CommonTree) ast.getChild(idx++);
            if ("distinct".equalsIgnoreCase(child.getText())) {
                distinct = true;
                child = (CommonTree) ast.getChild(idx++);
            } else {
                distinct = false;
            }
            List<IExpression> arguments = new ArrayList<IExpression>();
            while (idx < ast.getChildCount()) {
                IExpression argument = create(child);
                arguments.add(argument);
                child = (CommonTree) ast.getChild(idx++);
            }
            this.arguments = Collections.unmodifiableList(arguments);
        }
    }

    public String getName() {
        return name;
    }

    public boolean areDistinctArguments() {
        return distinct;
    }

    public List<IExpression> getArguments() {
        return arguments;
    }

    public boolean isAll() {
        return all;
    }

    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(getName());
        buffer.append(" (");
        if (isAll()) {
            buffer.append('*');
        } else {
            if (areDistinctArguments()) {
                buffer.append("DISTINCT ");
                for (int idx = 0; idx < getArguments().size(); idx++) {
                    if (idx > 0) {
                        buffer.append(", ");
                    }
                    buffer.append(getArguments().get(idx));
                }
            }
        }
        buffer.append(')');
        return buffer.toString();
    }
}
